package com.atguigu.gulimail.ware.listener;

import com.atguigu.common.to.mq.OrderTo;
import com.atguigu.gulimail.ware.service.WareSkuService;
import com.atguigu.gulimail.ware.vo.mq.StockLockedTo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author fangy
 * @date 2022-10-22 16:52
 */
@Service
@RabbitListener(queues = "stock.release.stock.queue")
public class Rabbitlistener {


    @Autowired
    WareSkuService wareSkuService;


    @RabbitHandler
    public void unlockStock(StockLockedTo to, Message message, Channel channel) throws IOException {

        System.out.println("收到库存解锁的消息");

        try{
            wareSkuService.unlockStock(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
        }
    }

    @RabbitHandler
    public void unlockStock(OrderTo to, Message message, Channel channel) throws IOException {

        System.out.println("订单关闭准备解锁库存");

        try{
            wareSkuService.CloseStock(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
        }
    }

}
